package com.application.metroscanner.adapters;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.metroscanner.R;

public class RideHistoryListAdapter extends ArrayAdapter<String> {

	private final Context context;
	private final ArrayList<String> values;
	private static final String[] days = { "Sunday", "Monday", "Tuesday",
			"Wednesday", "Thursday", "Friday", "Saturday" };
	private static final String[] months = { "January", "February", "March",
			"April", "May", "June", "July", "August", "September", "October",
			"November", "December" };
	
	private static NumberFormat hourFormatter = new DecimalFormat("##");

	public RideHistoryListAdapter(Context context, ArrayList<String> values) {
		super(context, R.layout.view_ride_history_list, values);
		this.context = context;
		this.values = values;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View rowView = convertView;
		if (rowView == null) {
			rowView = inflater.inflate(R.layout.view_ride_history_list, parent,
					false);
		}
		// format : dayOfWeek;month;day;hour;minute;time;rating
		String[] split = values.get(position).split(";", 0);

		// date

		String dateText = days[Integer.parseInt(split[0]) - 1] + " "
				+ months[Integer.parseInt(split[1])] + " " + split[2] + ", "
				+ split[3] + "h" + split[4] + "mn";
		TextView dateLine = (TextView) rowView.findViewById(R.id.dateLine);
		dateLine.setText(dateText);

		// ride time
		TextView rideTimeLine = (TextView) rowView.findViewById(R.id.rideTime);
		long rideTime = Long.parseLong(split[5]);
		long diffMinutes = rideTime / (60 * 1000) % 60;
		long diffHours = rideTime / (60 * 60 * 1000) % 24;
		String timeText = hourFormatter.format(diffHours) + "h " + hourFormatter.format(diffMinutes) + "mn";
		rideTimeLine.setText(timeText);

		TextView rating = (TextView) rowView.findViewById(R.id.rating);
		rating.setText(split[6]);
		return rowView;
	}
}
